/**
 * Created by Flagship on 2017/5/9.
 */
define(['angular'], function (angular) {

    /*-----------------------------------------
     *   ����ʼǷ���
     ------------------------------------------*/
    angular.module('books.service.info', [])
        .constant('infoServiceConfig', {
            urls: {
                getbookAll: 'http://47.92.95.246:8012/book/info/getall',//获取所有图书
                deletebook: 'http://47.92.95.246:8012/book/info/delete',//删除图书
                createbook: 'http://47.92.95.246:8012/book/info/add',//新增图书
                updatebook: 'http://47.92.95.246:8012/book/info/update',//更新图书
                getSinglebook: 'http://47.92.95.246:8012/book/info/search',//查询图书(用于查看和更新)
                searchbook: 'http://47.92.95.246:8012/book/info/search',//（此方法需要后台增加接口（多条件查询））
                uploadImg: 'http://47.92.95.246:8012/book/info/upload/image'//上传图片

            }
        })
        .factory('infoService', ['$http', '$q', 'infoServiceConfig', function ($http, $q, infoServiceConfig) {
            return {
                getbookAll: function () {
                    var deffered = $q.defer();
                    $http({
                        method: 'GET',
                        dataType: "json",
                        contentType: 'application/json; charset=UTF-8',
                        url: infoServiceConfig.urls.getbookAll
                    })
                        .success(function (response) {
                            deffered.resolve(response);
                        })
                        .error(function (e, code) {
                            deffered.reject(code);
                        });
                    return deffered.promise;
                },
                deletebook: function (id) {
                    //内置服务 同步请求数据
                    var deffered = $q.defer();
                    //请求数据
                    $http({
                        method: 'POST',
                        dataType: "json",
                        contentType: 'application/json; charset=UTF-8',
                        url: infoServiceConfig.urls.deletebook,
                        params: {
                            id: id
                        }
                    })
                    .success(function (response) {
                        deffered.resolve(response);
                    })
                    .error(function (e, code) {
                        deffered.reject(code);
                    });
                    return deffered.promise;
                },
                createbook: function (model) {
                    var deffered = $q.defer();
                    $http({
                        method: 'POST',
                        url: infoServiceConfig.urls.createbook,
                        data: model,
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                        transformRequest: function (obj) {
                            var str = [];
                            for (var s in obj) {
                                str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                            }
                            return str.join("&");
                        }
                    })
                        .success(function (response) {
                            deffered.resolve(response);
                            console.log(response);
                        })
                        .error(function (e, code) {
                            deffered.reject(code);
                        });
                    return deffered.promise;
                },
                updatebook: function (model) {
                    var deffered = $q.defer();
                    $http({
                        method: 'POST',
                        url: infoServiceConfig.urls.updatebook,
                        data: model,
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                        transformRequest: function (obj) {
                            var str = [];
                            for (var s in obj) {
                                str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                            }
                            return str.join("&");
                        }
                    })
                        .success(function (response) {
                            deffered.resolve(response);
                            console.log(response);
                        })
                        .error(function (e, code) {
                            deffered.reject(code);
                        });
                    return deffered.promise;
                },
                getSinglebook: function (id) {
                    var deffered = $q.defer();
                    $http({
                        method: 'GET',
                        dataType: "json",
                        contentType: 'application/json; charset=UTF-8',
                        url: infoServiceConfig.urls.getSinglebook,
                        params: {
                            id: id
                        }
                    })
                        .success(function (response) {
                            deffered.resolve(response);
                        })
                        .error(function (e, code) {
                            deffered.reject(code);
                        });
                    return deffered.promise;
                },
                searchbook: function (params) {
                    var deffered = $q.defer();
                    $http({
                        method: 'GET',
                        dataType: "json",
                        contentType: 'application/json; charset=UTF-8',
                        url: infoServiceConfig.urls.searchbook,
                        params: {
                            name: params.name,
                            author: params.author,
                            publisher: params.publisher,
                            isbn: params.isbn,
                            language: params.language
                        }
                    })
                        .success(function (response) {
                            deffered.resolve(response);
                        })
                        .error(function (e, code) {
                            deffered.reject(code);
                        });
                    return deffered.promise;
                },
                uploadImg: function (file, id) {
                    var deffered = $q.defer();
                    $http({
                        method: 'POST',
                        data: {
                            file: file,
                            id: id
                        },
                        url: infoServiceConfig.urls.uploadImg,
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                        transformRequest: function (obj) {
                            var str = [];
                            for (var s in obj) {
                                str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                            }
                            return str.join("&");
                        }
                    })
                        .success(function (response) {
                            deffered.resolve(response);
                        })
                        .error(function (e, code) {
                            deffered.reject(code);
                        });
                    return deffered.promise;
                },
            };
        }]);

});

